package com.orangelife.mobile.hourlyEmployee.activity;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.util.OrangePhoneUtil;
import com.curry.android.http.HttpUtil;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.confirmOrder.activity.PayConfirmOrderActivity;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.hourlyEmployee.entity.ServiceInformationBean;
import com.orangelife.mobile.parkingcard.biz.ParkingSpaceHttpBiz;
import com.orangelife.mobile.util.DateUtil;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;

import org.json.JSONObject;

public class OrderDetailActivity extends OrangeLifeBaseActivity {
	
	/**标题中间*/
	private TextView tvTitle;
	/** 标题右边*/
	private TextView tvTitleRight;
	/** 名字*/
	private TextView tvName;
	/**电话*/
	private TextView tvPhoneNumber;
	/** 地址*/
	private TextView tvAddress;
	/**订单编号*/
	private TextView tvOrderNumber;
	/**订单状态*/
	private TextView tvStatus;
	/** 服务价格*/
	private TextView tvServicePrice;
	/**服务时间*/
	private TextView tvServiceTime;
	/**订单总额*/
	private TextView tvOrderTotal;
	/**抵用券*/
	private TextView tvVoucher;
	/**备注*/
	private TextView tvRemark;
	/**联系*/
	private TextView tvContact;
	/**总价*/
	private TextView tvTotal;
	/**取消订单*/
	private TextView tvCancelOrder;
	/**确认订单*/
	private TextView tvConfirmOrder;
	/**取消订单按钮*/
	private Button btnCancelOrder;
	/**订单总额*/
	private LinearLayout llOrderTotal;
	/**抵用券*/
	private LinearLayout llVoucher;
	/**联系*/
	private LinearLayout llContact;
	/**合计*/
	private LinearLayout llTotal;
	/**备注的linearLayout*/
	private LinearLayout llRemark;
	/**抵用券下面的View*/
	private View viewVoucherButtom;
	/**服务时间下面的View*/
	private View viewServiceTimeButtom;
	/**备注下面的View*/
	private View viewRemarkButtom;
	/**接收从后台传来的数据*/
	private Map<String,Object> mapDetail;
	/**列表数据*/
	private Map<String,Object> map;
	private Dialog dialog;
	/**弹框内容*/
	private String dialogMessage = "";
	/**弹框左边按钮字*/
	private String dialogBtnLeft = "";
	int status = 0;
	private int isfirm = 3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hourly_employee_order_detial);
		initeView();
		findView();
	}
	
	/**
	 * 初始化
	 */
	private void initeView(){
		tvTitle = (TextView)findViewById(R.id.tv_title);
		tvTitleRight = (TextView)findViewById(R.id.tv_title_right);
		tvName = (TextView)findViewById(R.id.tv_username);
		tvPhoneNumber = (TextView)findViewById(R.id.tv_userphone);
		tvAddress = (TextView)findViewById(R.id.tv_address);
		tvOrderNumber = (TextView)findViewById(R.id.tvOrderNumber);
		tvServicePrice = (TextView)findViewById(R.id.tvServicePrice);
		tvServiceTime = (TextView)findViewById(R.id.tvServiceTimeContent);
		tvStatus = (TextView)findViewById(R.id.tvOrderStatus);
		tvOrderTotal = (TextView)findViewById(R.id.tvOrderTotal);
		tvVoucher = (TextView)findViewById(R.id.tvVoucher);
		tvRemark = (TextView)findViewById(R.id.tvRemarks);
		tvContact = (TextView)findViewById(R.id.tvContact);
		tvTotal = (TextView)findViewById(R.id.tvTotal);
		tvCancelOrder = (TextView)findViewById(R.id.tvCancelOrder);
		tvConfirmOrder = (TextView)findViewById(R.id.tvConfirmOrder);
		btnCancelOrder = (Button)findViewById(R.id.btnCancelOrder);
		llOrderTotal = (LinearLayout)findViewById(R.id.llOrderTotal);
		llVoucher = (LinearLayout)findViewById(R.id.llVoucher);
		llContact = (LinearLayout)findViewById(R.id.llContact);
		llTotal = (LinearLayout)findViewById(R.id.llTotal);
		viewRemarkButtom = (View)findViewById(R.id.viewRemarkButtom);
		viewServiceTimeButtom = (View)findViewById(R.id.viewServiceTimeButtom);
		viewVoucherButtom = (View)findViewById(R.id.viewVoucherButtom);
		llRemark = (LinearLayout)findViewById(R.id.llRemark);
		
		
	}
	
	/**
	 * 赋值
	 */
	private void findView(){
		tvTitle.setText(R.string.order_detail);
		tvTitleRight.setVisibility(View.INVISIBLE);
		btnCancelOrder.setOnClickListener(listener);
		tvCancelOrder.setOnClickListener(listener);
		tvConfirmOrder.setOnClickListener(listener);
		tvContact.setOnClickListener(listener);
		Intent intent = getIntent();
		map = (Map<String, Object>) intent.getSerializableExtra("detail");
		dialog = DialogHelper.getInstance().createLoadingDialog(
				OrderDetailActivity.this, Constant.LOADING);
		dialog.show();
		getDetail();
	}
	
	/**
	 * 设置详情
	 */
	
	private void setDetail(){
		tvName.setText(mapDetail.get("shipName").toString());
		tvPhoneNumber.setText(mapDetail.get("shipPhone").toString());
		tvAddress.setText(mapDetail.get("shipAddr").toString());
		tvOrderNumber.setText(mapDetail.get("serviceOrderID").toString());
		tvServicePrice.setText(mapDetail.get("price").toString());
		String time = DateUtil.getInstance().tranStringForDate_33(mapDetail.get("serviceDate").toString());
		tvServiceTime.setText(time+" "+mapDetail.get("hours").toString()+"小时");
		String remark = mapDetail.get("remark").toString();
		String couponValue = mapDetail.get("couponValue").toString();
		if("0".equals(couponValue)){
			llVoucher.setVisibility(View.GONE);
		}else{
			tvVoucher.setText(mapDetail.get("couponValue").toString()+"元抵用券");
		}
		tvOrderTotal.setText("¥"+mapDetail.get("totalCost").toString());
		tvTotal.setText("合计：¥"+mapDetail.get("totalCost").toString());
		if(!StringUtil.isBlank(remark)){
			tvRemark.setText(mapDetail.get("remark").toString());
		}else{
			llRemark.setVisibility(View.GONE);
		}
		String statusStr = mapDetail.get("status").toString();
		if(!StringUtil.isBlank(statusStr)){
			status = Integer.parseInt(statusStr);
		}
		switch (status) {
		//受理中
		case 1:
			statusStr = "受理中";
			viewServiceTimeButtom.setVisibility(View.GONE);
			llOrderTotal.setVisibility(View.GONE);
			llVoucher.setVisibility(View.GONE);
			viewVoucherButtom.setVisibility(View.GONE);
			viewRemarkButtom.setVisibility(View.GONE);
			llContact.setVisibility(View.GONE);
			llTotal.setVisibility(View.GONE);
			btnCancelOrder.setVisibility(View.VISIBLE);
			break;
			//待上门
		case 2:
			statusStr = "待上门";
			viewServiceTimeButtom.setVisibility(View.GONE);
			llOrderTotal.setVisibility(View.GONE);
			llVoucher.setVisibility(View.GONE);
			llContact.setVisibility(View.VISIBLE);
			viewVoucherButtom.setVisibility(View.GONE);
			btnCancelOrder.setVisibility(View.GONE);
			llTotal.setVisibility(View.VISIBLE);
			break;
			//已完成
		case 5:
			statusStr = "已完成";
			btnCancelOrder.setVisibility(View.GONE);
			llTotal.setVisibility(View.GONE);
			llContact.setVisibility(View.GONE);
			break;
			//已取消
		case 3:
			statusStr = "已取消";
			viewServiceTimeButtom.setVisibility(View.GONE);
			llOrderTotal.setVisibility(View.GONE);
			llVoucher.setVisibility(View.GONE);
			viewVoucherButtom.setVisibility(View.GONE);
			viewRemarkButtom.setVisibility(View.GONE);
			llContact.setVisibility(View.GONE);
			llTotal.setVisibility(View.GONE);
			btnCancelOrder.setVisibility(View.GONE);
			break;
			//待取消
		case 4:
			statusStr = "待取消";
			viewServiceTimeButtom.setVisibility(View.GONE);
			llOrderTotal.setVisibility(View.GONE);
			llVoucher.setVisibility(View.GONE);
			llContact.setVisibility(View.VISIBLE);
			viewVoucherButtom.setVisibility(View.GONE);
			viewRemarkButtom.setVisibility(View.GONE);
			llTotal.setVisibility(View.GONE);
			btnCancelOrder.setVisibility(View.GONE);
			break;

		default:
			break;
		}
		tvStatus.setText(statusStr);
	}
	/**
	 * 监听器
	 */
	
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.btnCancelOrder:
				isfirm = 0;
				dialogMessage = getResources().getString(R.string.sure_cancel_order);
				dialogBtnLeft = getResources().getString(R.string.cancel_order);
				isOrderDialog();
				break;
			case R.id.tvCancelOrder:
				isfirm = 0;
				dialogMessage = getResources().getString(R.string.hourly_employee_cancel_tip);
				dialogBtnLeft = getResources().getString(R.string.cancel_order);
				isOrderDialog();
				break;
			case R.id.tvConfirmOrder:
				isfirm = 1;
				dialogMessage = getResources().getString(R.string.confirm_service_complete);
				dialogBtnLeft = getResources().getString(R.string.firm);
				isOrderDialog();
				break;
			case R.id.tvContact:
				OrangePhoneUtil.callCustomerService2(OrderDetailActivity.this);
				break;
			default:
				break;
			}
		}
	};
	
	/**
	 * 弹出对话框
	 */
	private void isOrderDialog1() {
		AlertDialog.Builder builder = new Builder(OrderDetailActivity.this);
		builder.setMessage(dialogMessage);
		builder.setPositiveButton(dialogBtnLeft, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				//取消订单
				if(isfirm == 0){
					cancelOrder();
				}else if(isfirm == 1){
					firmOrder();
				}

				return;
			}
		});
		builder.setNegativeButton("返回", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	private void isOrderDialog() {
		final ConfirmDialog dialog = new ConfirmDialog(OrderDetailActivity.this, "温馨提示", dialogMessage, "返回", dialogBtnLeft);
		dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
			@Override
			public void doLeft() {
				dialog.dismiss();
			}

			@Override
			public void doRight() {
				dialog.dismiss();
				//取消订单
				if(isfirm == 0){
					cancelOrder();
				}else if(isfirm == 1){
					firmOrder();
				}
				return;
			}
		});

		dialog.show();
	}


	/**
	 * 取消订单
	 * 
	 */
	private void cancelOrder() {
		dialog.show();
		Map<String, Object> mapSubmit = new HashMap<String, Object>();
		mapSubmit.put("serviceOrderID", mapDetail.get("serviceOrderID").toString());
		JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.URL_HOURLY_EMPLOYEE_CANCEL_ORDER, mapSubmit, handler, 7, HttpUtil.ACTION_URI);
	}
	
	/**
	 * 确认订单
	 */
	private void firmOrder() {
		ExitApplication.getInstance().addPaidActivity(OrderDetailActivity.this);
		Intent intent = new Intent();
		intent.putExtra("orderInfo", "钟点工服务"+mapDetail.get("price").toString()+"(x"+mapDetail.get("hours").toString()+"小时)");
		intent.putExtra("Payment", mapDetail.get("totalCost").toString());
		intent.putExtra("type", 7);
		intent.putExtra("serviceOrderID", mapDetail.get("serviceOrderID").toString());
		intent.setClass(OrderDetailActivity.this, PayConfirmOrderActivity.class);
		startActivity(intent);
		}
	
	/**
	 * 获取详情
	 */
	
	private void getDetail(){
		String serviceOrderID = null;
		Map<String, Object> map = new HashMap<String, Object>();
		if(this.map != null && this.map.size() != 0){
			serviceOrderID = this.map.get("serviceOrderID").toString();
		}else{
			Intent intent = getIntent();
			serviceOrderID = intent.getStringExtra("jumpuri");
			if(StringUtil.isBlank(serviceOrderID)){
				serviceOrderID = intent.getStringExtra("id");
			}
			if("1".equals(serviceOrderID)){
				serviceOrderID = ServiceInformationBean.getServiceOrderID();
			}
		}
		map.put("{serviceOrderID}", serviceOrderID);
		JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.URL_HOURLY_EMPLOYEE_DETAIL, map, handler, 1, HttpUtil.ACTION_URI);
	}
	
	/**
	 * 接收处理线程返回的消息
	 */
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.WHAT_ERROR_HTTP:
				ToastHelper.getInstance()._toast(String.valueOf(msg.obj));
				break;
			case 1:
				mapDetail = new HashMap<String, Object>();

				mapDetail = (Map<String, Object>) JSONHelper.jsonToMap(
						msg.obj.toString()).get("entity");
				DialogHelper.closeDialog(dialog);
				setDetail();
				break;
			case 7:
				String errInfo = JSONHelper.jsonToMap(msg.obj.toString())
				.get("errInfo").toString();
				if (errInfo.equals("OK")){
					DialogHelper.closeDialog(dialog);
					ToastHelper.getInstance()._toast(getResources().getString(R.string.cancel_success));
					IntentHelper.getInstances().getIntent(OrderDetailActivity.this, OrderListActivity.class);
				    finish();
				    ExitApplication.getInstance().finishPaidActivity();
				}else{
					DialogHelper.closeDialog(dialog);
					ToastHelper.getInstance()._toast(
							getResources().getString(
									R.string.cancel_fail));
				}
				break;
			}
			if (dialog != null) {
				dialog.dismiss();
			}
		};
	};
	
}
